/*
 *  CS202 - Spring 2009">
 *  Professor: Dr. Michael Peterson">
 *  Student: Deylo Woo (dwoo711@gmail.com) ">
 */

package p4startercode;

import javax.swing.*;

/**
 *
 * @author Mike Peterson
 */
public class ClownPause implements Runnable {

    private boolean paused;
    private int length;
    JButton b1;
    JButton b2;
    String icon1, icon2;

    /**
     * Create a pause object that will reset the icon of two given buttons after
     * the pause completes.
     * @param b1 A button whose icon needs to be reset
     * @param b2 Another button whose icon needs to be reset
     * @param icon1 A string containing the location of an image file representing
     * an icon to paint onto button b1 after the pause completes.
     * @param icon2 A string containing the location of an image file representing
     * an icon to paint onto button b2 after the pause completes.
     */
    public ClownPause(JButton b1, JButton b2,
            String icon1, String icon2)
    {
        paused = false;
        this.b1 = b1;
        this.b2 = b2;
        this.icon1 = icon1;
        this.icon2 = icon2;
    }

    /**
     * Create a pause object that will reset the icon of a given button after
     * the pause completes.
     * @param b1 The button whose icon needs to be reset
     * @param icon1 A string containing the location of an image file representing
     * an icon to paint onto the button after the pause completes.
     */
    public ClownPause(JButton b1,String icon1)
    {
        this(b1,null,icon1,null);
    }

    /**
     * Create a pause object that will not reset any icons after pauses.
     */
    public ClownPause()
    {
        paused = false;
        b1 = null;
        b2 = null;
        icon1 = null;
        icon2 = null;
    }

    /**
     * Determine whether the pauser is currently being used to implement a
     * pause operation.
     * @return true if the thread is currently sleeping, false if the thread
     * is not currently in use.
     */
    public boolean isPaused()
    {
        return paused;
    }

    
    /**
     * Runs a ClownPause thread for a set number of milliseconds.
     * @param milli Amount of time to run the pause thread.
     * @return true if the pause will run, false if it won't.  The pause won't
     * run if the thread is already paused.
     */
    public boolean startPause(int milli)
    {
        if(milli < 1) throw new IllegalArgumentException(
                "milli must be positive");

        // if we are already in the paused state, do nothing.
        if(paused) return false;
        
        // otherwise, spawn a thread that will pause for a set amount of time
        length = milli;
        Thread thrd = new Thread(this);
        thrd.start();
        return true;
    }

    /**
     * Runs a ClownPause thread for a set number of milliseconds.  At the end of
     * the pause, an image's icon will be reset to the icon at the given
     * location
     * @param milli Amount of time to run the pause thread.
     * @param b1 A button whose icon will be changed at the end of the pause.
     * @param icon1 A file location containing an image for an icon to paint
     * onto a button at the end of the pause.
     * @return true if the pause will run, false if it won't.  The pause won't
     * run if the thread is already paused.
     */
    public boolean startPause(int milli, JButton b1, String icon1)
    {
        this.b1 = b1;
        this.icon1 = icon1;
        return startPause(milli);
    }

    /**
     * Runs a ClownPause thread for a set number of milliseconds.  At the end of
     * the pause, two images' icons will be reset to the icons at the given
     * locations
     * @param milli Amount of time to run the pause thread.
     * @param b1 A button whose icon will be changed at the end of the pause.
     * @param icon1 A file location containing an image for an icon to paint
     * onto button b1 at the end of the pause.
     * @param b2 A button whose icon will be changed at the end of the pause.
     * @param icon2 A file location containing an image for an icon to paint
     * onto button b2 at the end of the pause.
     * @return true if the pause will run, false if it won't.  The pause won't
     * run if the thread is already paused.
     */
    public boolean startPause(int milli, JButton b1, String icon1,
                JButton b2, String icon2)
    {
        this.b1 = b1;
        this.icon1 = icon1;
        this.b2 = b2;
        this.icon2 = icon2;
        return startPause(milli);
    }

    /**
     * Run a thread to implement the pause.  At the end of the pause, image's
     * icons will be reset if the images are not null.
     */
    public void run()
    {
        // indicate that a pause is currently in use
        paused = true;
        try{
            Thread.sleep(length);  // sleep for a predetermined number of
            // milliseconds
        } catch(InterruptedException ie) {}



        // if there are Buttons with icons to update, update them
        if(b1 != null && icon1 != null)
        {
            b1.setIcon(new ImageIcon( icon1 ));
        }

        if(b2 != null && icon2 != null)
        {
            b2.setIcon(new ImageIcon( icon2 ));
        }

        // IMPORTANT: set image pointers to null after updating buttons' icons
        b1 = null;
        b2 = null;
        icon1 = null;
        icon2 = null;

        // when done sleeping and updating buttons, reset paused state
        paused = false;
    }

}
